package cn.dujc.superkid.activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
import cn.dujc.core.adapter.BaseQuickAdapter;
import cn.dujc.core.ui.BaseListActivity;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.MessageAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.MessageResult;
import cn.dujc.superkid.bean.MyExamineResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.utils.Preferences;

// TODO: 2018/8/16 0016 接口数据添加可能会有问题，等待有数据在修改 
public class MessageActivity extends BaseListActivity {
    private RecyclerView recyclerView;
    private final List<MessageResult> list = new ArrayList<>();
    private MessageAdapter messageAdapter;

    @Override
    public void initBasic(Bundle savedInstanceState) {
        super.initBasic(savedInstanceState);
        setTitle("我的消息");
    }

    @Nullable
    @Override
    protected BaseQuickAdapter initAdapter() {
        messageAdapter = new MessageAdapter(list);
        return messageAdapter;
    }

    @Override
    protected void onItemClick(int position) {

    }

    @Override
    protected void loadMore() {
        refreshDone();
        loadDone(true, true);
    }

    @Override
    protected void reload() {
        loadData();
    }

    private void loadData() {
        if (!Preferences.isLogin(this)) {
            starter().go(LoginActivity.class);
        }
        final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
        // public Call<Result<MessageResult>> memberMessage(@QueryMap QMap map);
        HttpHelper.getUserCenter().memberMessage(QMap.build()
                .put("user_id", loginResult.getUid())
                .put("channel",  Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("logincode", loginResult.getLogincode())
                .create()).enqueue(new ResultCallBack<Result<MessageResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<MessageResult> body) {
                 list.clear();
                final MessageResult data = body.getData();
                if (data != null) {
                    if (data != null) {
                        MessageActivity.this.list.add(data);
                    }else{
                            messageAdapter.setEmptyView(R.layout.layout_empty_img);
                    }
                }
                notifyDataSetChanged(true);
            }
            @Override
            public void onFailureCallback(int code, String msg) {
                super.onFailureCallback(code, msg);
                loadFailure();
                if(code==400){
                    messageAdapter.setEmptyView(R.layout.layout_empty_img);
                }
            }
        });
    }
}
